package com.example.computer_exam_training_sys.service.impl;

import com.example.computer_exam_training_sys.entity.UserFavoritesInfo;
import com.example.computer_exam_training_sys.mapper.UserFavoritesMapper;
import com.example.computer_exam_training_sys.service.UserFavoritesService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;

@Service
public class UserFavoritesServiceImpl  implements UserFavoritesService {

    @Autowired
    private UserFavoritesMapper mapper;

    @Autowired
    private ObjectMapper objectMapper;

    @Override
    public List<UserFavoritesInfo> findAll() {
        return mapper.findAll();
    }

    @Override
    public List<UserFavoritesInfo> findByUserId(Integer userId) {
        return mapper.findByUserId(userId);
    }

    @Override
    public UserFavoritesInfo findById(Integer id) {
        return mapper.findById(id);
    }

    @Override
    public UserFavoritesInfo findByUserAndQuestion(Integer userId, Integer questionId) {
        return mapper.findByUserAndQuestion(userId, questionId);
    }

    @Override
    public int insert(UserFavoritesInfo favorite) {
        return mapper.insert(favorite);
    }

    @Override
    public int deleteById(Integer id) {
        return mapper.deleteById(id);
    }

    @Override
    public int deleteByUserAndQuestion(Integer userId, Integer questionId) {
        return mapper.deleteByUserAndQuestion(userId, questionId);
    }

    @Override
    public boolean existsByUserAndQuestion(Integer userId, Integer questionId) {
        UserFavoritesInfo favorite = mapper.findByUserAndQuestion(userId, questionId);
        return favorite != null;
    }

    @Override
    public List<Map<String, Object>> findFavoritesWithDetail(Integer userId, Integer courseId, Integer pointId, String startTime, String endTime) {
        return mapper.findFavoritesWithDetail(userId, courseId, pointId, startTime, endTime);
    }

    @Override
    public List<Map<String, Object>> findFavoritesByIds(List<Integer> ids) {
        List<Map<String, Object>> list = mapper.findFavoritesByIds(ids);
        for (Map<String, Object> q : list) {
            Object answerObj = q.get("answer");
            if (answerObj != null && answerObj instanceof String) {
                String answerStr = (String) answerObj;
                try {
                    Map<String, Object> ans = objectMapper.readValue(answerStr, Map.class);
                    // 选择题
                    if (ans.containsKey("options")) {
                        Object opts = ans.get("options");
                        java.util.List<Map<String, Object>> optionList = new java.util.ArrayList<>();
                        if (opts instanceof java.util.Map) {
                            // 兼容{"A":"xxx","B":"yyy"}格式
                            java.util.Map optMap = (java.util.Map) opts;
                            for (Object key : optMap.keySet()) {
                                Map<String, Object> optionItem = new java.util.HashMap<>();
                                optionItem.put("label", String.valueOf(key));
                                optionItem.put("value", String.valueOf(key));
                                optionItem.put("text", String.valueOf(optMap.get(key)));
                                optionList.add(optionItem);
                            }
                        } else if (opts instanceof java.util.List) {
                            // 标准数组格式
                            char label = 'A';
                            for (Object opt : (java.util.List<?>) opts) {
                                Map optMap = (Map) opt;
                                String text = optMap.get("text") != null ? String.valueOf(optMap.get("text")) :
                                    (optMap.get("content") != null ? String.valueOf(optMap.get("content")) :
                                    (optMap.get("option") != null ? String.valueOf(optMap.get("option")) : ""));
                                Map<String, Object> optionItem = new java.util.HashMap<>();
                                optionItem.put("label", String.valueOf(label));
                                optionItem.put("value", String.valueOf(label));
                                optionItem.put("text", text);
                                optionList.add(optionItem);
                                label++;
                            }
                        }
                        q.put("options", optionList);
                        q.put("type", "choice");
                    } else {
                        q.put("type", "subjective");
                    }
                    // 标准答案
                    if (ans.containsKey("correct")) {
                        q.put("correctAnswer", ans.get("correct"));
                    } else {
                        q.put("correctAnswer", "");
                    }
                } catch (JsonProcessingException e) {
                    q.put("type", "subjective");
                    q.put("correctAnswer", "");
                }
            } else {
                q.put("type", "subjective");
                q.put("correctAnswer", "");
            }
        }
        return list;
    }
}
